package pzl.com.library.renderer;

import android.graphics.Canvas;
import android.graphics.Path;
import android.util.Log;

import java.util.HashMap;

import pzl.com.library.chart.IChart;
import pzl.com.library.chart.LineChart;
import pzl.com.library.data.BaseData;
import pzl.com.library.data.DataList;
import pzl.com.library.data.Entry;
import pzl.com.library.model.XBounds;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-22]
 */
public class LineDataRenderer extends BaseDataRenderer {
    private static final String TAG = "LineDataRenderer";

    public LineDataRenderer() {
        super();
        mDrawPaint.setTextSize(12);
        mDrawPaint.setStrokeWidth(1);
    }

    @Override
    public void drawData(Canvas c, IChart chart) {
        Log.d(TAG, "LineDataRenderer drawData");

        BaseData baseData = chart.getData();
        Path path = new Path();

        XBounds xBounds = ((LineChart) chart).getXBounds();
        if (!xBounds.isDrawEnable()) return;
        Log.d(TAG, " xBounds.minVisibleIndex =" + xBounds.getMinVisibleIndex() + ",xBounds.maxVisibleIndex=" + xBounds.getMaxVisibleIndex());

        HashMap<String, DataList> allData = baseData.getAllData();
        for (String key : allData.keySet()) {
            DataList dataList = allData.get(key);
            path.reset();
            for (int i = xBounds.getMinVisibleIndex(); i <= xBounds.getMaxVisibleIndex(); i++) {//该可视范围为数据最长的datalist的可视范围，对应短的datalist不要绘制
                int realMaxIndex = dataList.maxIndex();
                if (i > realMaxIndex) continue;
                Entry entry = dataList.getEntry(i);
                path.lineTo(entry.xPX(), entry.yPX());
            }
            mDrawPaint.setColor(dataList.getPrimaryColor());
            c.drawPath(path, mDrawPaint);
        }

        /*for (int i = xBounds.getMinVisibleIndex(); i <= xBounds.getMaxVisibleIndex(); i++) {
            for (String key : allData.keySet()) {
                path.reset();
                DataList dataList = allData.get(key);
                for (int j = 0; j <= dataList.size() - 1; j++) {
                    Entry entry = dataList.getEntry(j);
                    path.lineTo(entry.xPX(), entry.yPX());
                }
                c.drawPath(path, mDrawPaint);
            }
        }*/
    }

}
